package com.yisoo.ubag.product.interceptor;


import com.alibaba.fastjson.TypeReference;
import com.yisoo.common.constant.LoginStatusConstant;
import com.yisoo.common.utils.R;
import com.yisoo.common.vo.LoginStatusVo;
import com.yisoo.ubag.product.feign.RenFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.concurrent.TimeUnit;
//@Component
public class LoginUserInterceptor implements HandlerInterceptor {
//    @Autowired
//    RenFeignService renFeignService;
//    @Autowired
//    StringRedisTemplate stringRedisTemplate;
//    public static ThreadLocal<LoginStatusVo> loginUser = new ThreadLocal<>();


//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        return true;
//
////      放行：
//        String uri = request.getRequestURI();
//        boolean match = new AntPathMatcher().match("/feign/**", uri);
////        System.out.println("OOOO");
//        if(match){
//            return true;
//        }
//
//        String token = null;
//        String userid = null;
////        token = getRequestToken((HttpServletRequest) request);
////        还是空的找cookie
////        if (StringUtils.isBlank(token)){
////方法一：通过遍历的方法进行遍历
//        Cookie[] cookies = request.getCookies();
//        if (cookies == null){
//            System.out.println("因为没cookie请求被拦截了");
//            return  false;
//        }
//        for (Cookie c : cookies) {
////            System.out.println(c.getName());
//            if(c.getName().equals("token")){
//                token = c.getValue();
//            }
//            if(c.getName().equals("userid")){
//                userid = c.getValue();
//            }
////            }
//        }
//        //        还是空的就算了
//        if(StringUtils.isBlank(token)){
//            System.out.println("no token");
//            return false;
//        }
////        System.out.println("qqq = "+userid + "[]"+token);
//        //        TODO: 这里更新了，由于请求带有userid所以取消去数据库查userid的繁琐操作
////        if (stringRedisTemplate.hasKey(token)){
////             userid = stringRedisTemplate.opsForValue().get(token);
////        }else{
////            userid = renFeignService.getUseridByToken(token).getData("userid", new TypeReference<String>() {
////            });
////            stringRedisTemplate.opsForValue().set(token, userid,60*30, TimeUnit.SECONDS);
////        }
//        //终于把userid取出来了
//        loginUser.set( new LoginStatusVo(Long.parseLong(userid),token));
//        //放行
//        return true;
//    }

    /**
     * 获取请求的token
     */
//    private String getRequestToken(HttpServletRequest httpRequest){
//        //从header中获取token
//        String token = httpRequest.getHeader("token");
//
//        //如果header中不存在token，则从参数中获取token
//        if(StringUtils.isBlank(token)){
//            token = httpRequest.getParameter("token");
//        }
//
//        return token;
//    }

}
